package com.jry.iaspacctivemq.active;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @author sss
 */
@Component
@Slf4j
public class QueueConsumerListener {

    /**
     * 消费监听
     */
    @JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListener")
    public void receiveQueue(ActiveMQMessage message, Session session) throws JMSException {
        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
//            ObjectMapper objectMapper = new ObjectMapper();
//            XXX xxx = objectMapper.readValue(textMessage.getText(), XXX.class);
//            logger.info("queue1接收到：{}", xxx);
            log.info("queue1接收到：{}", textMessage.getText());
            try {
                //todo 业务逻辑，成功后手动确认
                message.acknowledge();
            } catch (JMSException e) {
                // 此不可省略 重发信息使用
                session.recover();
            }
        }
    }
}
